﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Active.Modules.Social;
using DotNetNuke.Modules.ActiveSocialMigrator.Data;
using System.Text;
using DotNetNuke.Security.Roles;
using DotNetNuke.Framework;
using DotNetNuke.Services.Localization;

namespace DotNetNuke.Modules.ActiveSocialMigrator
{
    public partial class Groups : ActiveSocialMigratorModuleBase
    {
        override protected void OnInit(EventArgs e)
        {
       //     InitializeComponent();
            base.OnInit(e);
        }

        //private void InitializeComponent()
        //{
        //    this.Load += new System.EventHandler(this.Page_Load);
        //    btnRun.Click += btnRun_Click;
        //}
        protected void Page_Load(object sender, EventArgs e)
        {
            ServicesFramework.Instance.RequestAjaxAntiForgerySupport();
            BindGroups();
            if (!Page.IsPostBack)
            {
                StringBuilder sb = new StringBuilder();
                var groupsDB = new GroupsDB();
                List<GroupInfo> groups = groupsDB.Groups_List(PortalId, 0, int.MaxValue, -1, UserId, false, "", "", 0, "", 0, -1);
                foreach (GroupInfo group in groups)
                {
                    sb.Append("<li>");
                    sb.Append("<input type=\"checkbox\" id=\"group-" + group.GroupId + "\" checked=\"checked\" />");
                    sb.Append(group.GroupName);
                    sb.Append(" (" + group.MemberCount + ")");
                    sb.Append("</li>");

                }
                litGroups.Text = sb.ToString();
            }
        }
        //protected void btnRun_Click(object sender, EventArgs e)
        //{
        //    var groupsDB = new GroupsDB();
        //    List<GroupInfo> groups = groupsDB.Groups_List(PortalId, 0, int.MaxValue, -1, UserId, false, "", "", 0, "", 0, -1);
        //    StringBuilder sb = new StringBuilder();
        //    RoleController roleController = new RoleController();
        //    Security.PortalSecurity ps = new Security.PortalSecurity();

        //    foreach (GroupInfo group in groups)
        //    {
        //        var roleInfo = roleController.GetRoleByName(PortalId, group.GroupName);
        //        if (roleInfo == null)
        //        {
        //            sb.Append("Converted: " + group.GroupName);
        //            sb.Append("<br />");
        //        } else
        //        {
        //            sb.Append("Error: Role Exists -> " + group.GroupName);
        //            sb.Append("<br />");
        //        }
                
                 
        //    }
        //    //litOutput.Text = sb.ToString();
        //}
        public RoleInfo ConvertGroupToRole(GroupInfo group)
        {
            RoleInfo roleInfo = new RoleInfo()
            {
                PortalID = PortalId,
                RoleName = group.GroupName,
                Description = group.GroupDescription,
                SecurityMode = SecurityMode.SocialGroup,
                Status = RoleStatus.Approved
            };
            bool reviewMembers = false;
            if (group.AccessType == GroupAccessTypes.GroupOpen)
            {
                roleInfo.IsPublic = true;
            } else if (group.AccessType == GroupAccessTypes.GroupClosed)
            {
                roleInfo.IsPublic = true;
                reviewMembers = true;
            } else
            {
                roleInfo.IsPublic = false;
            }



            return roleInfo;
        }
        private void BindGroups()
        {
            var arrGroups = RoleController.GetRoleGroups(PortalId);

            drpRoleGroup.Items.Add(new ListItem(Localization.GetString("GlobalRoles"), "-1"));

            foreach (RoleGroupInfo roleGroup in arrGroups)
            {
                drpRoleGroup.Items.Add(new ListItem(roleGroup.RoleGroupName, roleGroup.RoleGroupID.ToString()));
            }
        }
    }
}